# -*- coding: UTF-8 -*-
"""
@Project :xlx-fastapi
@File    :admin_dao.py
@Author  :xlx-ali
@Date    :2024/06/24 13:24 星期一
"""

from app.dao.models import XlxAdmin
from .base_dao import getDatabaseSession


class AdminQueryDao(object):
    """用户查询类dao"""

    @classmethod
    def findByUserName(cls, username: str) -> XlxAdmin:
        """单条查询示例"""
        with getDatabaseSession() as session:
            query = session.query(XlxAdmin).filter(XlxAdmin.username == username)
            result = query.first()
        return result


class AdminOperateDao(object):
    """操作用户相关dao"""

    @classmethod
    def save(cls, user: XlxAdmin):
        with getDatabaseSession(False) as session:
            session.add(user)
            session.commit()
            session.refresh(user)
        return user

    @classmethod
    def updataAdmin(cls, user: XlxAdmin) -> XlxAdmin:
        """更新数据"""
        with getDatabaseSession() as session:
            query = session.query(XlxAdmin).filter(XlxAdmin.id == user.id).first()
            query.last_login_time = user.last_login_time
            query.last_login_ip = user.last_login_ip,
            query.last_login_ipaddress = user.last_login_ipaddress
            session.commit()
            session.refresh(query)

        return query
